/**
 * Company : SIANGKLOM.
 * Created By : Chatree Siangklom
 * Created On : Oct 18, 2013  3:36:30 PM 
 * e-Mail : dr_chatree@hotmail.com
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : com.tis.database.dao.staging.impl
 * File Name : ApplicationLogingDaoImpl.java
 * TODO
 */
package com.tis.database.dao.staging.impl;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;
import promis.utility.CommonService;
import com.tis.database.dao.impl.AbstractDaoImpl;
import com.tis.database.dao.staging.ApplicationLogingDao;
import com.tis.database.entitys.staging.ApplicationLoging;

/**
 * @author Chatree Siangklom of Administrator
 * Date =Oct 18, 2013 : 3:36:30 PM 
 * CreateBy=Chatree Siangklom
 * 
 * ApplicationLogingDaoImpl
 * TODO
 */
@SuppressWarnings("unchecked")
@Repository
public class ApplicationLogingDaoImpl extends AbstractDaoImpl<ApplicationLoging, String> implements ApplicationLogingDao {
	private static final Logger logging = LoggerFactory.getLogger(ApplicationLogingDaoImpl.class);
	
	/**
	 * Date =Oct 18, 2013 : 3:37:46 PM 
	 * CreateBy=Chatree Siangklom
	 * ApplicationLogingDaoImpl
	 * TODO
	 * @param entityClass
	 */
	protected ApplicationLogingDaoImpl() {
		super(ApplicationLoging.class);
	}	

	/** (Javadoc)
	 * @see com.tis.database.dao.staging.ApplicationLogingDao#Search(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, int)
	 * Date =Oct 18, 2013 : 3:37:43 PM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 */
	@Override
	public List<ApplicationLoging> Search(	String id, 
											String instanceNo,
											String msgCode, 
											String msgDesc, 
											String msgAltDesc,
											String refInstanceNo, 
											String createdOnStart, 
											String createdOnEnd,
											int maxResult) throws HibernateException, Exception {

		try {
			
			Session session = getCurrentSession();
			Query q = session.getNamedQuery(ApplicationLoging.findBySearch);
			String query = q.getQueryString();
			
			if(!CommonService.IsNull(id))
				query += " And upper(c.id) like '%"+ CommonService.IsNullRString(id).toUpperCase() +"%'";
			
			if(!CommonService.IsNull(instanceNo))
				query += " And upper(c.instanceNo) like '%"+ CommonService.IsNullRString(instanceNo).toUpperCase() +"%' ";
			
			if(!CommonService.IsNull(msgCode))
				query += " And upper(c.msgCode) like '%"+ CommonService.IsNullRString(msgCode).toUpperCase() +"%'";
			
			if(!CommonService.IsNull(msgDesc))
				query += " And upper(c.msgDesc) like '%"+ CommonService.IsNullRString(msgDesc).toUpperCase() +"%'";
			
			if(!CommonService.IsNull(msgAltDesc))
				query += " And upper(c.msgAltDesc) like '%"+ CommonService.IsNullRString(msgAltDesc).toUpperCase() +"%'";
			
			if(!CommonService.IsNull(refInstanceNo))
				query += " And upper(c.refInstanceNo) like '%"+ CommonService.IsNullRString(refInstanceNo).toUpperCase() +"%'";
			
			if(!CommonService.IsNull(createdOnStart) && !CommonService.IsNull(createdOnEnd))
				query += " And to_char(c.createdon, 'YYYY-MM-DD') BETWEEN :createdOnStart And :createdOnEnd ";
			
			query += " Order By c.id DESC ";
			
			logging.info(query);
			
			q = session.createQuery(query);
			
			if(!CommonService.IsNull(createdOnStart))
				q.setParameter("createdOnStart", CommonService.IsNullRString(createdOnStart).toUpperCase());
			
			if(!CommonService.IsNull(createdOnEnd))
				q.setParameter("createdOnEnd", CommonService.IsNullRString(createdOnEnd).toUpperCase());
			
			if(maxResult > 0)
				q.setMaxResults(maxResult);			
			
			List<ApplicationLoging> lst = Collections.checkedList(q.list(), ApplicationLoging.class);
			if(CommonService.getSize(lst) > 0 )
				return lst;
			else 
				return null;
			
		}catch(HibernateException ex) {
			logging.error(ex.getMessage());
			ex.printStackTrace();
			throw ex;
		}
		catch(Exception ex1) {
			logging.error(ex1.getMessage());
			ex1.printStackTrace();
			throw ex1;
		}
	}

	/** (Javadoc)
	 * @see com.tis.database.dao.staging.ApplicationLogingDao#NextSequence()
	 * Date =Oct 18, 2013 : 3:37:43 PM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 */
	@Override
	public BigDecimal NextSequence() throws HibernateException, Exception {

		return super.NextSequence("APPLICATION_LOGING");
	}
	
	/** (Javadoc)
	 * @see com.tis.database.dao.impl.AbstractDaoImpl#saveOrUpdate(java.lang.Object)
	 * Date =Oct 18, 2013 : 3:40:57 PM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 */
	@Override
	public void saveOrUpdate(ApplicationLoging e) throws HibernateException {
		// TODO Auto-generated method stub
		super.saveOrUpdate(e);
	}
	
	/** (Javadoc)
	 * @see com.tis.database.dao.impl.AbstractDaoImpl#saveOrUpdate(java.util.List)
	 * Date =Oct 18, 2013 : 3:41:04 PM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 */
	@Override
	public void saveOrUpdate(List<ApplicationLoging> e) throws HibernateException {
		// TODO Auto-generated method stub
		super.saveOrUpdate(e);
	}

}
